package cn.jacksigxu.min3halla.loot;

import cn.jacksigxu.min3halla.init.MHBlockStateProperties;
import cn.jacksigxu.min3halla.init.MHLootFunctionTypes;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.functions.LootItemConditionalFunction;
import net.minecraft.world.level.storage.loot.functions.LootItemFunctionType;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;

public class SetAlcoholNbtFunction extends LootItemConditionalFunction {

    private final IntegerProperty property = MHBlockStateProperties.ALCOHOL;

    public SetAlcoholNbtFunction(LootItemCondition[] conditions) {
        super(conditions);
    }

    @Override
    public ItemStack run(ItemStack stack, LootContext context) {
        BlockState state = context.getParamOrNull(LootContextParams.BLOCK_STATE);
        if (state != null && state.hasProperty(property)) {
            int alcohol = state.getValue(property);
            if (alcohol <= 0) return stack;
            CompoundTag tag = stack.getOrCreateTag();
            tag.putInt("Alcohol", alcohol);
        }
        return stack;
    }

    @Override
    public LootItemFunctionType getType() {
        return MHLootFunctionTypes.SET_ALCOHOL_NBT.get();
    }

    public static LootItemConditionalFunction.Builder<?> of() {
        return simpleBuilder(SetAlcoholNbtFunction::new);
    }

    public static class Serializer extends LootItemConditionalFunction.Serializer<SetAlcoholNbtFunction> {

        public void serialize(JsonObject pJson, SetAlcoholNbtFunction function, JsonSerializationContext pSerializationContext) {
            super.serialize(pJson, function, pSerializationContext);
        }

        public SetAlcoholNbtFunction deserialize(JsonObject pObject, JsonDeserializationContext pDeserializationContext, LootItemCondition[] pConditions) {
            return new SetAlcoholNbtFunction(pConditions);
        }
    }
}